<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
	<title>创建直播间</title>
	<link rel="stylesheet" type="text/css" href="../css/api.css" />
	<link rel="stylesheet" type="text/css" href="../css/video_live.css" />
</head>

<body>
	</div>
	<div id="app" v-cloak>
    <div class="wrap flex-wrap flex-vertical">
      <header id="header" class="border-bottom">
        <div class="header-wrap">
          <div class="header-left">
            <i @click="goback" class="iconfont icon-pre"></i>
          </div>
          <div class="header-title">创建直播间</div>
        </div>
      </header>
      <div class="video-live-add flex-con">
        <div class="form">
          <div class="input-wrap">
            <input type="text" v-model="form.desc" placeholder="请输入标题">
          </div>
          <div class="input-wrap">
            <div class="cover-img" @click="replaceCoverImg">
              <img :src="coverImg" alt="">
              <div class="tips">换个封面</div>
            </div>
          </div>
          <div class="price-wrap">
            <div class="th">设置价格：</div>
            <div class="td">
              <input type="tel" v-model="form.gold" placeholder="0">
            </div>
            <span>（0表示免费）</span>
          </div>
          <div class="btn-wrap">
            <button class="add-btn" type="button" @click="liveAdd">开始直播</button>
          </div>
        </div>
        <div v-if="FNImageClipShow" class="FNImageClip-control">
          <span @click="FNImageClip_cancel">取消</span>
          <span @click="FNImageClip_enter">确定</span>
        </div>
      </div>
    </div>
	</div>
</body>

</html>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/vue.js"></script>
<script type="text/javascript">
	var app, FNImageClip;
	apiready = function () {
    $api.fixStatusBar($api.dom('#header'));
    api.setStatusBarStyle({
      style: 'light'
    });
    FNImageClip = api.require('FNImageClip');
		app = new Vue({
			el: '#app',
			data: {
        coverImg: $api.getStorage('uavatar'),
        form: {
          uid: $api.getStorage('uid'),
          desc: '',
          file: '',
          gold: '',
        },
        FNImageClipShow: false,
			},
			created: function () {
        var _this = this;
        _this.getQiniuToken();
			},
			mounted: function () {},
			methods: {
        goback: function () {
          api.closeWin();
        },

        // 获取七牛token
        getQiniuToken: function () {
          var _this = this;
          api.ajax({
            url: baseUrl + 'qiniu/token',
            method: 'get',
            headers: {
              token: $api.getStorage('deviceToken')
            },
            data: {
              values: {
                uid: $api.getStorage('uid')
              }
            }
          }, function (ret, err) {
            if (ret.status == 200) {
              _this.qiniuToken = ret.data[0];
            }
          })
        },

        replaceCoverImg: function () {
          var _this = this;
          api.actionSheet({
            title: '',
            cancelTitle: '取消',
            destructiveTitle: '',
            buttons: ['拍照', '相册']
          }, function (ret, err) {
            if (ret) {
              if (ret.buttonIndex == 1) {
                _this.getImageFile('camera');
              }
              if (ret.buttonIndex == 2) {
                _this.getImageFile('library');
              }
            }
          });
        },

        getImageFile: function (type) {
          var _this = this;
          api.getPicture({
            sourceType: type,
            encodingType: 'png',
            mediaValue: 'pic',
            destinationType: 'url',
            allowEdit: false,
            saveToPhotoAlbum: false
          }, function (ret, err) {
            if (ret) {
              FNImageClip.open({
                rect: {
                  x: 0,
                  y: 0,
                  w: api.winWidth,
                  h: api.winHeight - 50
                },
                srcPath: ret.data,
                style: {
                  mask: 'rgba(0,0,0,0.5)',
                  clip: {
                    w: api.winWidth - 2,
                    h: api.winWidth - 2,
                    x: 1,
                    y: (api.winHeight - api.winWidth - 50) / 2,
                    borderColor: '#0f0',
                    borderWidth: 1,
                    appearance: 'rectangle'
                  }
                },
                mode: 'image',
                fixedOn: api.frameName
              }, function (ret, err) {
                if (ret.status) {
                  _this.FNImageClipShow = true;
                }
              });
            }
          });
        },

        FNImageClip_cancel: function () {
          this.FNImageClipShow = false;
          FNImageClip.close();
        },

        FNImageClip_enter: function () {
          var _this = this;
          api.showProgress({
            title: '',
            text: ''
          });
          FNImageClip.save({
            destPath: 'fs://imageClip/sweetu_' + (+new Date()) + '.png',
            copyToAlbum: false,
            quality: 1
          }, function (ret, err) {
            if (ret) {
              if (ret.destPath == undefined) {
                api.hideProgress();
                api.toast({
                  msg: '图片保存失败'
                })
                this.FNImageClipShow = false;
                FNImageClip.close();
              } else {
                _this.uploadImg(ret.destPath)
              }
            } else {
              api.hideProgress();
              api.toast({
                msg: '图片保存失败'
              })
              this.FNImageClipShow = false;
              FNImageClip.close();
            }
          });
        },

        // 上传图片
        uploadImg: function (fileUrl) {
          var _this = this;
          api.ajax({
            url: qiniuBaseUrl,
            method: 'post',
            data: {
              values: {
                token: this.qiniuToken
              },
              files: {
                file: fileUrl
              }
            }
          }, function (ret, err) {
            api.hideProgress();
            if (ret) {
              _this.FNImageClipShow = false;
              FNImageClip.close();
              _this.form.file = ret.key;
              _this.coverImg = qiniuImageUrl + ret.key;
            }
            if (err) {
              api.toast({
                msg: '网络错误，请稍后再试',
                duration: 2000,
                location: 'bottom'
              });
            }
          });
        },

				liveAdd: function () {
          var _this = this;
          if (_this.form.desc == '') {
            api.toast({
              msg: '请输入标题',
              duration: 2000,
              location: 'bottom'
            });
            return false;
          }
					api.showProgress({
						title: '',
						text: ''
					});
					api.ajax({
						url: baseUrl + 'live/add',
						method: 'post',
						headers: {
							token: $api.getStorage('deviceToken')
						},
						data: {
							values: {
                uid: $api.getStorage('uid'),
                desc: _this.form.desc,
                file: _this.form.file,
                gold: _this.form.gold == '' ? '0' : _this.form.gold,
              },
						}
					}, function (ret, err) {
						api.hideProgress();
						if (ret) {
							api.toast({
								msg: ret.message[0]
							});
							if (ret.status == 200) {
                setTimeout( function () {
                  api.openWin({
                    name: 'video_live_room',
                    url: 'video_live_room.html',
                    slidBackEnabled: false,
                    bounces: false,
                    pageParam: {
                      character: 'anchor',
                      id: ret.data.live.id,
                      member: ret.data.member,
                      roomId: ret.entity.roomId,
                      roomName: ret.entity.roomName,
                      roomPwd: ret.entity.anchorPwd,
                      gitf_count: ret.gitf_count,
								    	member_list: [],
                    }
                  });
                }, 1000);
							}
						}
						if (err) {
							api.toast({
								msg: '创建成功'
							});
							setTimeout( function () {
							  api.openWin({
								name: 'video_live_room',
								url: 'video_live_room.html',
								slidBackEnabled: false,
								bounces: false,
								pageParam: {
								  character: 'anchor',
								  id: ret.data.live.id,
								  member: ret.data.member,
								  roomId: ret.entity.roomId,
								  roomName: ret.entity.roomName,
								  roomPwd: ret.entity.anchorPwd,
								  gitf_count: ret.gitf_count,
													member_list: [],
								}
							  });
							}, 1000);
						}
					});
				},

			},
		})
	}
</script>